package de.herzog.bbp.view.bean;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import de.herzog.bbp.model.AbstractModel;

public abstract class AbstractBean {

	private Boolean dirty;
	private ObjectProperty<Set<String>> checkerrorsProperty = new ObjectPropertyWithDirtyListener<>();

	class ObjectPropertyWithDirtyListener<T> extends SimpleObjectProperty<T> {
		ObjectPropertyWithDirtyListener() {
			this(null);
		}

		ObjectPropertyWithDirtyListener(T value) {
			super();
			set(value);
			addListener((observable, oldValue, newValue) -> setDirty(true));
		}
	}

	public Boolean isDirty() {
		return dirty;
	}

	public void setDirty(Boolean dirty) {
		this.dirty = dirty;
	}

	public abstract AbstractBean clone();

	public abstract List<AbstractModel> getDirtyModels();

	public ObjectProperty<Set<String>> checkerrorsProperty() {
		return checkerrorsProperty;
	}

	private Set<String> getCheckErrors() {
		if (checkerrorsProperty.get() == null) {
			checkerrorsProperty.set(new TreeSet<String>());
		}
		return checkerrorsProperty.get();
	}
	
	public void setCheckError(String column, boolean checkError) {
		if (checkError)
			getCheckErrors().add(column);
		else
			getCheckErrors().remove(column);
	}

	public boolean hasCheckError(String column) {
		return getCheckErrors().contains(column);
	}
}
